# _*_ coding:utf-8 _*_
# By:赵先宇
import requests
import json
import time

KEY = 'ASDASSDASDASD'
import hashlib
# 方案一： url上携带key 判断key是否相等
# 缺点： 不动态 不安全
# r1 = requests.post(
#     url='http://127.0.0.1:8000/test/',
#     params={'key': KEY},
#     data=json.dumps({'k1': 'v1'}).encode('utf-8'),
#     headers={'Content-Type': 'application/json'}
# )
# print(r1.text)

ctime = time.time()

# 方案二：动态生成key
# md5加密
def gen_key():
    key = "{}|{}".format(KEY, ctime)
    md5 = hashlib.md5()
    md5.update(key.encode('utf-8'))

    return md5.hexdigest()

key = gen_key()

print(key)

r1 = requests.post(
    url='http://127.0.0.1:8000/test/',
    params={'key': key, 'ctime': ctime},
    data=json.dumps({'k1': 'v1'}).encode('utf-8'),
    headers={'Content-Type': 'application/json'}
)
print(r1.text)